package com.haikesi.api.excutionplan.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.excutionplan.domain.po.ExcutionPlanPO;
import com.haikesi.api.excutionplan.domain.po.ExcutionPlanProcessPO;
import com.haikesi.api.excutionplan.domain.vo.*;
import com.haikesi.api.excutionplan.service.IExcutionPlanProcessService;
import com.haikesi.api.excutionplan.service.IExcutionPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description = "执行计划相关接口", tags = ApiIndex.ExcutionPlanProcessController)
@RequestMapping(value = "/api/exeutionPlanProcess")
@RestController
public class ExcutionPlanProcessController {

    @Autowired
    private IExcutionPlanProcessService service;

    @ApiOperation(value = "查询执行计划进程列表")
    @GetMapping(value = "/list")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<ExcutionPlanVO>>> list(@ApiParam(value = "查询参数") @ModelAttribute ExcutionPlanProcessSearchVO excutionPlanSearchVO) {
        List<ExcutionPlanProcessVO> list = service.getList(excutionPlanSearchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "添加执行计划进程")
    @PostMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R add(@ApiParam(value = "添加参数") @Valid @RequestBody ExcutionPlanProcessAddVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        ExcutionPlanProcessPO excutionPlanPO = service.addExcutionPlanProcess(vo);
        return new R(excutionPlanPO);
    }

    @ApiOperation(value = "修改执行计划进程")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R updateTimerTask(@ApiParam(value = "修改参数") @Valid @RequestBody ExcutionPlanProcessUpdateVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.updateExcutionPlanProcess(vo);
        return new R();
    }

    @ApiOperation(value = "删除执行计划进程")
    @DeleteMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R delete(@ApiParam(value = "执行计划进程ID") @RequestParam String id, String account, String localId) {
        ExcutionPlanProcessDeleteVO vo = new ExcutionPlanProcessDeleteVO();
        vo.setId(id);
        vo.setAccount(account);
        vo.setLocalId(localId);
        service.delete(vo);
        return new R();
    }
}
